<?php

    /**
     * 请求basic_api 基础类库
     */
namespace Common\Libiary;

    class BaseApiHttp {

        public $timeout = 25;

        public $connecttimeout = 10;

        public $ssl_verifypeer = FALSE;
    
        public $useragent = 'apex sdk';

        public $appid = 103;

        private $secret = '31e5f601b87a39a4fa3e0b6acaf789f8';

        private $secretkey = '01b1688f97f94776baae85d77b06048b';




        public function http ( $url, $method, $postfields = NULL, $headers = array()  ) {
            $this->http_info = array();
            $ci = curl_init();
            curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
            curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
            curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
            curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
            curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ci, CURLOPT_ENCODING, "gzip");
            curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
            curl_setopt($ci, CURLOPT_HEADER, FALSE);
            switch ($method) {

                case 'POST':
                    curl_setopt($ci, CURLOPT_POST, TRUE);
                    if (!empty($postfields)) {
                        curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
                        $this->postdata = $postfields;
                    }
                    break;
                case 'DELETE':
                    curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
                    if (!empty($postfields)) {
                        $url = "{$url}?{$postfields}";
                    }
            }
            curl_setopt($ci, CURLOPT_URL, $url );
            curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );
            curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
            $response = curl_exec($ci);

            $this->url = $url;

            $errno = curl_errno($ci);
            $info = curl_getinfo($ci);
            $log['url'] = $url;
            $log['params'] = is_array($postfields)? json_encode($postfields):$postfields;
            $log['response'] = $response;
            $log['info'] = json_encode($info);
            LogEX::i("curl_api_info",$log);

            if (!empty($errno)) {
                $data = array(
                    'url' => $url,
                    'errno' => $errno,
                    'errmsg' => curl_error($ci),
                    'postFields' => $postfields
                );
//                $Common = new common();
//                $Common->errlog($data);
            }
            if (empty($response)) {
                $info  = curl_getinfo($ci);
                $this->http_code = $info['http_code'];
                $this->http_info = array(
                    'url' => $info['url'],
                    'request_header' => isset($info['request_header']) ? $info['request_header'] : ''
                );

                if($method == 'POST'){
                    $this->http_info = array_merge($this->http_info, array('post_body'=>$this->postdata));
                }
            }
            curl_close ($ci);
            return $response;

        }

        public function https ( $url, $method, $postfields = NULL, $headers = array()  ) {
            $this->http_info = array();
            $ci = curl_init();
            curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
            curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
            curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
            curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
            curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ci, CURLOPT_ENCODING, "gzip");
            curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
            curl_setopt($ci, CURLOPT_HEADER, FALSE);
            switch ($method) {

                case 'POST':
                    curl_setopt($ci, CURLOPT_POST, TRUE);
                    if (!empty($postfields)) {
                        // curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
                        curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($postfields)); // Post提交的数据包
                        $this->postdata = $postfields;
                    }
                    break;
                case 'DELETE':
                    curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
                    if (!empty($postfields)) {
                        $url = "{$url}?{$postfields}";
                    }
            }
            curl_setopt($ci, CURLOPT_URL, $url );
            curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );
            curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
            $response = curl_exec($ci);

            $this->url = $url;
            $errno = curl_errno($ci);


            if (!empty($errno)) {
                $data = array(
                    'url' => $url,
                    'errno' => $errno,
                    'errmsg' => curl_error($ci),
                    'postFields' => $postfields
                );
//                $Common = new common();
//                $Common->errlog($data);
            }

            //var_dump( curl_errno($ci));
           // var_dump(curl_error($ci));

            if (empty($response)) {
                $info  = curl_getinfo($ci);
                $this->http_code = $info['http_code'];
                $this->http_info = array(
                    'url' => $info['url'],
                    'request_header' => isset($info['request_header']) ? $info['request_header'] : ''
                );

                if($method == 'POST'){
                    $this->http_info = array_merge($this->http_info, array('post_body'=>$this->postdata));
                }
            }
            curl_close ($ci);
            return $response;

        }

        function getHeader($ch, $header) {
            $i = strpos($header, ':');
            if (!empty($i)) {
                $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
                $value = trim(substr($header, $i + 2));
            }
            return strlen($header);
        }


        //生成签名算法
        public function build_params() {
            $str = "";
            $time = time();
            $params['appid'] = $this->appid;
            $params['nonce'] = $this->createNoncestr();
            $params['time'] = $time;
            ksort($params);
            foreach($params as $key => $val){
                $str .= $key.$val;
            }
            $str .= $this->secret;
            $md5 = md5($str);
            $params['sign'] = $md5;
            return $params;
        }

        //运营后台生成签名算法
        public function build_sparams($params=[]) {
            $str = "";
            $time = time();
            $params['appid'] = $this->appid;
            $params['nonce'] = rand(1000, 9999);
            $params['time'] = $time;
            ksort($params);
            foreach($params as $key => $val){
                $str .= $key.$val;
            }
            $str .= $this->secret;
            $md5 = md5($str);
            $params['sign'] = $md5;
            return $params;
        }

         function createSignCode($secretusernew){
             $params['time'] = time();
             $params['nonce'] = rand(1, 999);
            $str = "";
            ksort($params);
            foreach($params as $key => $val){
                $str .= $key.$val;
            }
            $str .= $secretusernew;
             $params['sign'] = md5($str);
            return $params;
        }

        //运营后台生成签名算法
        public function build_sparamsyy($params=[]) {
            $params['nonce'] = rand(1000, 9999);
            $params['time'] = time();
            ksort($params);
            $params['sign'] =  md5(http_build_query($params) . $this->secretkey);
            return $params;
        }

        /**
         * 优惠券
         * @param array $data
         * @param null $key
         * @return string
         */
        public function couponSignature(array $data, $key = null)
        {
            ksort($data);
            $str = '';
            foreach ($data as $k => $v)
                $str .= $k . $v;
            return md5($str . $key);
        }

        //生成随机串
        private function createNoncestr( $length = 8 )  {
            $chars = "abcdefghijklmnopqrstuvwxyz0123456789";  
            $str ="";
            for ( $i = 0; $i < $length; $i++ )  {  
                $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);  
            }  
            return $str;
        }
        

    }